<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Method (computer programming)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Method_(computer_programming)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Method_computer_programming rootpage-Method_computer_programming skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Method (computer programming)</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>A <b>method</b> in <a href="Object-oriented_programming" title="Object-oriented programming">object-oriented programming</a> (OOP) is a <a href="Procedure_(computer_science)" class="mw-redirect" title="Procedure (computer science)">procedure</a> associated with an <a href="Object_(computer_science)" title="Object (computer science)">object</a>, and generally also a <a href="Message_passing" title="Message passing">message</a>. An object consists of <i>state data</i> and <i>behavior</i>; these compose an <a href="Interface_(object-oriented_programming)" title="Interface (object-oriented programming)"><i>interface</i></a>, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.
</p><p>Data is represented as <a href="Property_(programming)" title="Property (programming)">properties</a> of the object, and behaviors are represented as methods. For example, a <code>Window</code> object could have methods such as <code>open</code> and <code>close</code>, while its state (whether it is open or closed at any given point in time) would be a property.
</p><p>In <a href="Class-based_programming" title="Class-based programming">class-based programming</a>, methods are defined within a <a href="Class_(computer_science)" class="mw-redirect" title="Class (computer science)">class</a>, and objects are <a href="Instance_(computer_science)" title="Instance (computer science)">instances</a> of a given class. One of the most important capabilities that a method provides is <i><a href="Method_overriding" title="Method overriding">method overriding</a></i> - the same name (e.g., <code>area</code>) can be used for multiple different kinds of classes. This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. A method in <a href="Java_(programming_language)" title="Java (programming language)">Java</a> programming sets the behavior of a class object. For example, an object can send an <code>area</code> message to another object and the appropriate formula is invoked whether the receiving object is a <code>rectangle</code>, <code>circle</code>, <code>triangle</code>, etc.
</p><p>Methods also provide the interface that other classes use to access and modify the properties of an object; this is known as <a href="Encapsulation_(computer_programming)" title="Encapsulation (computer programming)"><i>encapsulation</i></a>. Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Overriding_and_overloading">Overriding and overloading</h2></div>
<p><a href="Method_overriding" title="Method overriding">Method overriding</a> and <a href="Function_overloading" title="Function overloading">overloading</a> are two of the most significant ways that a method differs from a conventional procedure or function call. Overriding refers to a subclass redefining the implementation of a method of its superclass. For example, <code>findArea</code> may be a method defined on a shape class,<sup id="cite_ref-:0_2-0" class="reference"><a href="#cite_note-:0-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> <code>triangle</code>, etc. would each define the appropriate formula to calculate their area. The idea is to look at objects as "black boxes" so that changes to the internals of the object can be made with minimal impact on the other objects that use it. This is known as encapsulation and is meant to make code easier to maintain and re-use.
</p><p>Method overloading, on the other hand, refers to differentiating the code used to handle a message based on the parameters of the method. If one views the receiving object as the first parameter in any method then overriding is just a special case of overloading where the selection is based only on the first argument. The following simple Java example illustrates the difference:
</p>
<div class="mw-heading mw-heading2"><h2 id="Accessor,_mutator_and_manager_methods">Accessor, mutator and manager methods</h2></div>
<p><a href="Accessor_method" class="mw-redirect" title="Accessor method">Accessor methods</a> are used to read the data values of an object. Mutator methods are used to modify the data of an object. Manager methods are used to initialize and destroy objects of a class, e.g. constructors and destructors.
</p><p>These methods provide an <a href="Abstraction_layer" title="Abstraction layer">abstraction layer</a> that facilitates <a href="Encapsulation_(object-oriented_programming)" class="mw-redirect" title="Encapsulation (object-oriented programming)">encapsulation</a> and <a href="Modularity_(programming)" class="mw-redirect" title="Modularity (programming)">modularity</a>. For example, if a bank-account class provides a <code>getBalance()</code> accessor method to retrieve the current <a href="Balance_(accounting)" title="Balance (accounting)">balance</a> (rather than directly accessing the balance data fields), then later <a href="Revision_control" class="mw-redirect" title="Revision control">revisions</a> of the same code can implement a more complex mechanism for balance retrieval (e.g., a <a href="Database" title="Database">database</a> fetch), without the dependent code needing to be changed. The concepts of encapsulation and modularity are not unique to object-oriented programming. Indeed, in many ways the object-oriented approach is simply the logical extension of previous paradigms such as <a href="Abstract_data_types" class="mw-redirect" title="Abstract data types">abstract data types</a> and <a href="Structured_programming" title="Structured programming">structured programming</a>.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Constructors">Constructors</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Constructor_(computer_science)" class="mw-redirect" title="Constructor (computer science)">Constructor (computer science)</a></div>
<p>A <a href="Constructor_(computer_science)" class="mw-redirect" title="Constructor (computer science)"><i>constructor</i></a> is a method that is called at the beginning of an object's lifetime to create and initialize the object, a process called <a href="Object_creation" class="mw-redirect" title="Object creation">construction</a> (or <i>instantiation</i>). Initialization may include an acquisition of resources. Constructors may have parameters but usually do not return values in most languages. See the following example in Java:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Main</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">_name</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_roll</span><span class="p">;</span>
<span class="w"> </span><span class="n">Main</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">roll</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// constructor method</span>
<span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">_roll</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">roll</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Destructor">Destructor</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Destructor_(computer_science)" class="mw-redirect" title="Destructor (computer science)">Destructor (computer science)</a></div>
<p>A <i><a href="Destructor_(computer_science)" class="mw-redirect" title="Destructor (computer science)">Destructor</a></i> is a method that is called automatically at the end of an object's lifetime, a process called <a href="Object_lifetime" title="Object lifetime">Destruction</a>. Destruction in most languages does not allow destructor method arguments nor return values. Destructors can be implemented so as to perform cleanup chores and other tasks at object destruction.
</p>
<div class="mw-heading mw-heading4"><h4 id="Finalizers">Finalizers</h4></div>
<p>In <a href="Garbage_collection_(computer_science)" title="Garbage collection (computer science)">garbage-collected</a> languages, such as <a href="Java_(programming_language)" title="Java (programming language)">Java</a>,<sup id="cite_ref-Bloch_4-0" class="reference"><a href="#cite_note-Bloch-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup><sup class="reference nowrap"><span title="Page / location: 26, 29">: 26, 29 </span></sup> <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>,<sup id="cite_ref-Albahari_5-0" class="reference"><a href="#cite_note-Albahari-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup><sup class="reference nowrap"><span title="Page / location: 208–209">: 208–209 </span></sup> and <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, destructors are known as <i><a href="Finalizer" title="Finalizer">finalizers</a></i>. They have a similar purpose and function to destructors, but because of the differences between languages that utilize garbage-collection and languages with manual memory management, the sequence in which they are called is different.
</p>
<div class="mw-heading mw-heading2"><h2 id="Abstract_methods">Abstract methods</h2></div>
<p>An <b>abstract method</b> is one with only a <a href="Method_signature" class="mw-redirect" title="Method signature">signature</a> and no <a href="Method_body" class="mw-redirect" title="Method body">implementation body</a>. It is often used to specify that a subclass must provide an implementation of the method, as in an <a href="Class_(computer_programming)#Abstract_and_concrete" title="Class (computer programming)">abstract class</a>. Abstract methods are used to specify <a href="Interface_(computing)" title="Interface (computing)">interfaces</a> in some programming languages.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Example">Example</h3></div>
<p><span class="mw-default-size" typeof="mw:File"></span>
</p><p>The following <a href="Java_(programming_language)" title="Java (programming language)">Java</a> code shows an abstract class that needs to be extended:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">abstract</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Shape</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">abstract</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">area</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">h</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">w</span><span class="p">);</span><span class="w"> </span><span class="c1">// abstract method signature</span>
<span class="p">}</span>
</pre></div>
<p>The following subclass extends the main class:
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Rectangle</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Shape</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nd">@Override</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">area</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">h</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">w</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">h</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">w</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Reabstraction">Reabstraction</h3></div>
<p>If a subclass provides an implementation for an abstract method, another subclass can make it abstract again. This is called <i>reabstraction</i>.
</p><p>In practice, this is rarely used.
</p>
<div class="mw-heading mw-heading4"><h4 id="Example_2">Example</h4></div>
<p>In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">IA</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">virtual</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">M</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">abstract</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">IB</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">IA</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">override</span><span class="w"> </span><span class="k">abstract</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">M</span><span class="p">();</span><span class="w"> </span><span class="c1">// allowed</span>
<span class="p">}</span>
</pre></div>
<p>Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="k">interface</span><span class="w"> </span><span class="n">IA</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">M</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">interface</span><span class="w"> </span><span class="n">IB</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">IA</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">abstract</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="n">IA</span><span class="p">.</span><span class="n">M</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">class</span><span class="w"> </span><span class="nc">C</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">IB</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// error: class 'C' does not implement 'IA.M'.</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Class_methods">Class methods</h2></div>
<p>Class methods are methods that are called on a <a href="Class_(computer_programming)" title="Class (computer programming)">class</a> rather than an instance. They are typically used as part of an object <a href="Meta-model" class="mw-redirect" title="Meta-model">meta-model</a>. I.e, for each class, defined an instance of the class object in the meta-model is created. <a href="Meta-object_protocol" class="mw-redirect" title="Meta-object protocol">Meta-model protocols</a> allow classes to be created and deleted. In this sense, they provide the same functionality as constructors and destructors described above. But in some languages such as the <a href="Common_Lisp_Object_System" title="Common Lisp Object System">Common Lisp Object System (CLOS)</a> the meta-model allows the developer to dynamically alter the <a href="Object_model" title="Object model">object model</a> at run time: e.g., to create new classes, redefine the class hierarchy, modify properties, etc.
</p>
<div class="mw-heading mw-heading2"><h2 id="Special_methods">Special methods</h2></div>
<p>Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. A language's compiler may automatically generate default special methods or a programmer may be allowed to optionally define special methods. Most special methods cannot be directly called, but rather the compiler generates code to call them at appropriate times.
</p>
<div class="mw-heading mw-heading3"><h3 id="Static_methods">Static methods</h3></div>
<div role="note" class="hatnote navigation-not-searchable">See also: <a href="Static_member_function" class="mw-redirect" title="Static member function">Static member function</a></div>
<p>Static methods are meant to be relevant to all the instances of a class rather than to any specific instance. They are similar to <a href="Static_variable" title="Static variable">static variables</a> in that sense. An example would be a static method to sum the values of all the variables of every instance of a class. For example, if there were a <code>Product</code> class it might have a static method to compute the average price of all products.
</p><p>A static method can be invoked even if no instances of the class exist yet. Static methods are called "static" because they are resolved at <a href="Compile_time" title="Compile time">compile time</a> based on the class they are called on and not dynamically as in the case with instance methods, which are resolved polymorphically based on the runtime type of the object.
</p>
<div class="mw-heading mw-heading4"><h4 id="Examples">Examples</h4></div>
<div class="mw-heading mw-heading5"><h5 id="In_Java">In Java</h5></div>
<p>In Java, a commonly used static method is:
</p>
<pre>Math.max(double a, double b)
</pre>
<p>This static method has no owning object and does not run on an instance. It receives all information from its arguments.<sup id="cite_ref-:0_2-1" class="reference"><a href="#cite_note-:0-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Copy-assignment_operators">Copy-assignment operators</h3></div>
<p>Copy-assignment operators define actions to be performed by the compiler when a class object is assigned to a class object of the same type.
</p>
<div class="mw-heading mw-heading3"><h3 id="Operator_methods">Operator methods</h3></div>
<p>Operator methods <a href="Operator_overloading" title="Operator overloading">define or redefine operator symbols</a> and define the operations to be performed with the symbol and the associated method parameters. C++ example:
</p>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="cp">#include</span><span class="w"> </span><span class="cpf"><string></span>
<span class="k">class</span><span class="w"> </span><span class="nc">Data</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="k">operator</span><span class="o"><</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="n">Data</span><span class="o">&</span><span class="w"> </span><span class="n">data</span><span class="p">)</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">roll_</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">roll_</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="n">Data</span><span class="o">&</span><span class="w"> </span><span class="n">data</span><span class="p">)</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">name_</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">name_</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">roll_</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">roll_</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="w"> </span><span class="n">name_</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">roll_</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Member_functions_in_C++">Member functions in C++</h2></div>
<p>Some procedural languages were extended with object-oriented capabilities to leverage the large skill sets and legacy code for those languages but still provide the benefits of object-oriented development. Perhaps the most well-known example is <a href="C%2B%2B" title="C++">C++</a>, an object-oriented extension of the <a href="C_(programming_language)" title="C (programming language)">C</a> programming language. Due to the design requirements to add the object-oriented paradigm on to an existing procedural language, message passing in C++ has some unique capabilities and terminologies. For example, in C++ a method is known as a <i>member function</i>. C++ also has the concept of <a href="Virtual_function" title="Virtual function">virtual functions</a> which are member functions that can be <a href="Method_overriding" title="Method overriding">overridden</a> in <a href="Derived_class" class="mw-redirect" title="Derived class">derived classes</a> and allow for <a href="Dynamic_dispatch" title="Dynamic dispatch">dynamic dispatch</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Virtual_functions">Virtual functions</h3></div>
<p><i><a href="Virtual_function" title="Virtual function">Virtual functions</a></i> are the means by which a C++ class can achieve polymorphic behavior. <i>Non-virtual member functions</i>, or <i>regular methods</i>, are those that do not participate in <a href="Polymorphism_(computer_science)" title="Polymorphism (computer science)">polymorphism</a>.
</p><p>C++ Example:
</p>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="cp">#include</span><span class="w"> </span><span class="cpf"><iostream></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><memory></span>
<span class="k">class</span><span class="w"> </span><span class="nc">Super</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="k">virtual</span><span class="w"> </span><span class="o">~</span><span class="n">Super</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">default</span><span class="p">;</span>
<span class="w"> </span><span class="k">virtual</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">IAm</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">"I'm the super class!</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Sub</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">Super</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">IAm</span><span class="p">()</span><span class="w"> </span><span class="k">override</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">"I'm the subclass!</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">};</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o"><</span><span class="n">Super</span><span class="o">></span><span class="w"> </span><span class="n">inst1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o"><</span><span class="n">Super</span><span class="o">></span><span class="p">();</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o"><</span><span class="n">Super</span><span class="o">></span><span class="w"> </span><span class="n">inst2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o"><</span><span class="n">Sub</span><span class="o">></span><span class="p">();</span>
<span class="w"> </span><span class="n">inst1</span><span class="o">-></span><span class="n">IAm</span><span class="p">();</span><span class="w"> </span><span class="c1">// Calls |Super::IAm|.</span>
<span class="w"> </span><span class="n">inst2</span><span class="o">-></span><span class="n">IAm</span><span class="p">();</span><span class="w"> </span><span class="c1">// Calls |Sub::IAm|.</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Property_(programming)" title="Property (programming)">Property (programming)</a></li>
<li><a href="Remote_method_invocation" class="mw-redirect" title="Remote method invocation">Remote method invocation</a></li>
<li><a href="Subroutine" class="mw-redirect" title="Subroutine">Subroutine</a>, also called subprogram, routine, procedure or function</li></ul>
<div class="mw-heading mw-heading2"><h2 id="Notes">Notes</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/tutorial/java/concepts/object.html">"What is an Object?"</a>. <i>oracle.com</i>. Oracle Corporation<span class="reference-accessdate">. Retrieved <span class="nowrap">13 December</span> 2013</span>.</cite></span>
</li>
<li id="cite_note-:0-2"><span class="mw-cite-backlink">^ <a href="#cite_ref-:0_2-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-:0_2-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite id="CITEREFMartin2009" class="citation book cs1"><a href="Robert_Cecil_Martin" class="mw-redirect" title="Robert Cecil Martin">Martin, Robert C.</a> (2009). <i>Clean Code: A Handbook of Agile Software Craftsmanship</i>. Prentice Hall. p. 296. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-13-235088-4</bdi>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite id="CITEREFMeyer1988" class="citation book cs1">Meyer, Bertrand (1988). <i>Object-Oriented Software Construction</i>. Cambridge: Prentice Hall International Series in Computer Science. pp. <span class="nowrap">52–</span>54. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-13-629049-3</bdi>.</cite></span>
</li>
<li id="cite_note-Bloch-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-Bloch_4-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFBloch2018" class="citation book cs1">Bloch, Joshua (2018). <i>"Effective Java: Programming Language Guide"</i> (third ed.). Addison-Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0134685991</bdi>.</cite></span>
</li>
<li id="cite_note-Albahari-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-Albahari_5-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFAlbahari" class="citation book cs1">Albahari, Joseph. <i>C# 10 in a Nutshell</i>. O'Reilly. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-098-12195-2</bdi>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html">"Abstract Methods and Classes"</a>. <i>oracle.com</i>. Oracle Java Documentation<span class="reference-accessdate">. Retrieved <span class="nowrap">11 December</span> 2014</span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239549316">
/* start https://en.wikipedia.org/ */
.mw-parser-output .refbegin{margin-bottom:0.5em}.mw-parser-output .refbegin-hanging-indents>ul{margin-left:0}.mw-parser-output .refbegin-hanging-indents>ul>li{margin-left:0;padding-left:3.2em;text-indent:-3.2em}.mw-parser-output .refbegin-hanging-indents ul,.mw-parser-output .refbegin-hanging-indents ul li{list-style:none}@media(max-width:720px){.mw-parser-output .refbegin-hanging-indents>ul>li{padding-left:1.6em;text-indent:-1.6em}}.mw-parser-output .refbegin-columns{margin-top:0.3em}.mw-parser-output .refbegin-columns ul{margin-top:0}.mw-parser-output .refbegin-columns li{page-break-inside:avoid;break-inside:avoid-column}@media screen{.mw-parser-output .refbegin{font-size:90%}}
/* end https://en.wikipedia.org/ */
</style><div class="refbegin" style="">
<ul><li><cite id="CITEREFJANA2005" class="citation book cs1">JANA, DEBASISH (1 January 2005). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=DnsM0WD-6iMC&pg=PA131"><i>C++ and Object-oriented Programming Paradigm</i></a>. PHI Learning Pvt. Ltd. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-81-203-2871-6</bdi>.</cite></li>
<li><cite id="CITEREFSengupta2004" class="citation book cs1">Sengupta, Probal (1 August 2004). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=ZLzt5WtsdzIC&pg=PA50"><i>Object-Oriented Programming: Fundamentals And Applications</i></a>. PHI Learning Pvt. Ltd. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-81-203-1258-6</bdi>.</cite></li>
<li><cite id="CITEREFSvenk2003" class="citation book cs1">Svenk, Goran (2003). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=Miq73i_J1i4C&pg=PA36"><i>Object-oriented Programming: Using C++ for Engineering and Technology</i></a>. Cengage Learning. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-7668-3894-3</bdi>.</cite></li>
<li><cite id="CITEREFBalagurusamy2013" class="citation book cs1">Balagurusamy (2013). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=WCHZAgAAQBAJ&pg=PA74"><i>Object Oriented Programming with C++</i></a>. Tata McGraw-Hill Education. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-259-02993-6</bdi>.</cite></li>
<li><cite id="CITEREFKirch-PrinzPrinz2002" class="citation book cs1">Kirch-Prinz, Ulla; Prinz, Peter (2002). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=-yhuY0Wg_QcC&pg=PA181"><i>A Complete Guide to Programming in C++</i></a>. Jones & Bartlett Learning. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-7637-1817-6</bdi>.</cite></li>
<li><cite id="CITEREFConger2006" class="citation book cs1">Conger, David (2006). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=1F6ipojt7DcC&pg=PA79"><i>Creating Games in C++: A Step-by-step Guide</i></a>. New Riders. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-7357-1434-2</bdi>.</cite></li>
<li><cite id="CITEREFSkinner1992" class="citation book cs1">Skinner, M. T. (1992). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=fgGLZ7WYxCMC&pg=PA97"><i>The Advanced C++ Book</i></a>. Silicon Press. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-929306-10-0</bdi>.</cite></li>
<li><cite id="CITEREFLove2005" class="citation book cs1">Love (1 September 2005). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=NXVkcCjPblcC&pg=PA18"><i>Linux Kernel Development</i></a>. Pearson Education. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-81-7758-910-8</bdi>.</cite></li>
<li><cite id="CITEREFDEHURIJAGADEVRATH2007" class="citation book cs1">DEHURI, SATCHIDANANDA; JAGADEV, ALOK KUMAR; RATH, AMIYA KUMAR (8 May 2007). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=fxUVrhjD4k0C&pg=PA78"><i>OBJECT-ORIENTED PROGRAMMING USING C++</i></a>. PHI Learning Pvt. Ltd. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-81-203-3085-6</bdi>.</cite></li></ul>
</div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2024-12-29" href="https://en.wikipedia.org/wiki/?title=Method_(computer_programming)&oldid=1265937912">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>